<?php

namespace StudyBuddy;

/**
 * Class for pagination of array
 * or mongo Cursor
 */
class Paginator extends StudyBuddyObject {

    protected $oRegistry;
    protected $oPager;

    public function __construct(Registry $oRegistry) {
        $this->oRegistry = $oRegistry;
    }

    /**
     * Getter for oPager object
     *
     * @return object Pager (from pear)
     */
    public function getPager() {
        return $this->oPager;
    }

    /**
     * Main entry method to paginate data
     * if mongo cursor is passed, it will
     * also set offset and limit on the cursor itself
     *
     * @param mixed int|array|object MongoCursor $arrData
     * @param int $perPage
     * @param array $arrExtraParams
     * 
     * @return array paged data
     *
     * @throws StudyBuddyDevException
     */
    public function paginate($arrData = null, $perPage, $arrExtraParams = array()) {

        $mongoCursor = null;

        if (2 > 1) { // was: FALSE == MOBILE_BROWSER_SETTINGS
            $arrParams = array('mode' => 'Sliding', 'fileName' => 'page%d.html', 'path' => '', 'append' => false, 'perPage' => $perPage, 'delta' => 2, 'urlVar' => 'pageID');

            if (!empty($arrData)) {
                if (is_array($arrData)) {
                    d('arrData: ' . print_r($arrData, true));
                    $arrParams['itemData'] = $arrData;
                } elseif (is_numeric($arrData)) {
                    d('totalItems: ' . $arrData);
                    $arrParams['totalItems'] = $arrData;
                } elseif (is_object($arrData) && ($arrData instanceof \MongoCursor)) {
                    d('got mongo cursor');
                    $mongoCursor = $arrData;
                    $arrParams['totalItems'] = $mongoCursor->count(true);
                    d('totalItems: ' . $arrParams['totalItems']);
                } else {
                    throw new DevException('wrong type for $arrData param. It must be array or numeric value. Passed: ' . gettype($arrData) . ' ' . get_class($addData));
                }
            }

            if (!empty($arrExtraParams)) {
                $arrParams = array_merge($arrParams, $arrExtraParams); // this way we can pass extra parameters here
            }

            include_once(STUDYBUDDY_PATH . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Pear' . DIRECTORY_SEPARATOR . 'Pager.php');
            include_once(STUDYBUDDY_PATH . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'Pear' . DIRECTORY_SEPARATOR . 'Pager' . DIRECTORY_SEPARATOR . 'Common.php');

            $this->oPager = \Pager::factory($arrParams);

            if (null !== $mongoCursor) {
                $curPage = $this->oPager->getCurrentPageID();
                d('curPage: ' . $curPage);
                $skip = ($this->oPager->getCurrentPageID() - 1) * $perPage;
                d('skip: ' . $skip);

                /**
                 * We skip if total > self::PER_PAGE
                 * meaning if we need pagination
                 * No need to skip if total is not greater than per page
                 * or if we are at first page (skip is 0 then)
                 *
                 */
                if (($skip > 0) && ($arrParams['totalItems'] > $perPage)) {
                    $mongoCursor->skip($skip);
                }

                $mongoCursor->limit($perPage);
            }

            $arrPagerData = $this->oPager->getPageData();


            d('$arrPagerData: ' . print_r($arrPagerData, true));
        } else {
            /**
             * @todo finish this
             * its is not complete yet and will not work properly
             */
            // setting for mobile browser pager
            $arrParams = array('mode' => 'Sliding', 'append' => false, 'perPage' => '10', 'delta' => 5, 'urlVar' => 'pageID', 'path' => $strPathForPaging, 'prevImg' => '&lt; Prev', 'nextImg' => 'Next &gt;', 'fileName' => '%d.htm', 'curPageSpanPre' => 'Pg. ', 'curPageSpanPost' => '', 'spacesAfterSeparator' => '0', 'spacesBeforeSeparator' => '0', 'itemData' => $arrRecentPosts);

            $this->oPager = \Pager::factory($arrParams);
            $arrData = $this->oPager->getPageData();
            $arrLinks = $this->oPager->getLinks();

            $arrPagerData = $arrData;
        }

        return $arrPagerData;
    }

// end paginateArray

    /**
     * Get HTML block with pagination links
     *
     * @todo translate word "Pages"
     *
     * @return string html with pagination links
     */
    public function getLinks() {
        $links = $this->oPager->getLinks();

        return (is_array($links) && !empty($links['all'])) ? '<div class="page"></ul>' . $links['all'] . '</ul></div>' : '';
    }

}
